﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Text;
using System.IO;
using MySql.Data;
using System.Data;
using MySql.Data.MySqlClient;

public partial class Report_Reportviewer : System.Web.UI.Page
{

    ReportDataSource dsRpt = new ReportDataSource();
    ReportParameter[] report_param;
    string FormName, xsd_name, report_path;
    DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && Request.QueryString.Count > 0)
            Fetch_Data();
        else
            return;
    }
    string reportQuery = "";
    private void Fetch_Data()
    {
        try
        {

            FormName = Request.QueryString["FormName"].ToString();
            switch (FormName)
            {
                #region Inventory Report
                case "Inventory":

                    // write query to fetch report 

                    dt = DBUtil.ExecuteDataTable("select 'SiliCon' as Product_Name,  "
                        + "10 AS QUANTITY_IN_KG ,5.00 As VALUE_PER_KG , 50.00 as TOTAL_VALUE,"
                        + " 'Current Inventory Value' as Report_Name "
                        + " UNION "
                        + "select 'Silinide' as Product_Name,  "
                        + "100 AS QUANTITY_IN_KG , 6.00 As VALUE_PER_KG , 600.00 as TOTAL_VALUE,"
                        + " 'Current Inventory Value' as Report_Name "

                        );


                    ReportDataSource rds = new ReportDataSource("DataSet1", dt);
                    // provide name of the Report xsd 
                    xsd_name = "DataSet1";

                    // add Multiple report Parameters seperated by Comma
                    report_param = new ReportParameter[] 
                    { 
                        new ReportParameter("ReportDate", DateTime.Now.ToString("dd MMM yyyy hh:mm")),
                        new ReportParameter("UserName", User.Identity.Name.ToString())
                    };


                    // provide report path
                    report_path = "Report/rptInventory.rdlc";
                    break;

                #endregion

                #region Sales Delivery Plan
                case "SalesDeliveryPlan":

                    // write query to fetch report 
                    reportQuery = "  SELECT 'SALES DELIVERY PLAN' Report_name,'Yogendra' Customer_name, "
             + " '123456' PO_NO, 'SICONIDE HP945' Product, "
             + " '1500'Volume_Ordered, 50 Price, 1500 Ordered_value, '500kg 31/03'Delivery_time, "
             + " '31 Mar' Planned_shipment,'04 APR' shipment_effected "
             + " UNION SELECT 'SALES DELIVERY PLAN' Report_name,'Yogendra' Customer_name, "
             + " '123456' PO_NO, 'SICONIDE HP945' Product, "
             + " '1500'Volume_Ordered,    50 Price, 1500 Ordered_value, '500kg 30/04'Delivery_time, "
             + " '30 Apr' Planned_shippment,'' shipment_effected "
             + " UNION SELECT 'SALES DELIVERY PLAN' Report_name,'Yogendra' Customer_name, "
             + " '123456' PO_NO, 'SICONIDE HP945' Product, "
             + " '1500'Volume_Ordered, 50 Price, 1500 Ordered_value, '500kg 31/05'Delivery_time, "
             + " '30 May' Planned_shippment,'' shipment_effected "
             + " UNION SELECT 'SALES DELIVERY PLAN' Report_name,'Deepak' Customer_name, "
             + " '125856' PO_NO, 'SICONIDE P95M' Product, "
             + " '300'Volume_Ordered,50 Price, 1500 Ordered_value, '150kg 31/03'Delivery_time, "
             + " '31 Mar' Planned_shippment,'' shipment_effected "
             + " UNION SELECT 'SALES DELIVERY PLAN' Report_name,'Deepak' Customer_name, "
             + " '125856' PO_NO, 'SICONIDE P95M' Product,'300'Volume_Ordered,"
             + " 50 Price, 1500 Ordered_value, '150kg 30/04'Delivery_time, "
             + " '31 Apr' Planned_shippment,'' shipment_effected ";

                    dt = DBUtil.ExecuteDataTable(reportQuery);



                    // provide name of the Report xsd 
                    xsd_name = "DataSet1";

                    // add Multiple report Parameters seperated by Comma
                    report_param = new ReportParameter[] 
                    { 
                        new ReportParameter("ReportDate", DateTime.Now.ToString("dd MMM yyyy hh:mm")),
                        new ReportParameter("UserName", User.Identity.Name.ToString())
                    };


                    // provide report path
                    report_path = "Report/rptSaleDeliveryPlan.rdlc";
                    break;

                #endregion

                #region Sales ProductionPlan
                case "ProductionPlan":

                    // write query to fetch report 
                    reportQuery =
 " SELECT * FROM "
+ " (SELECT ' w1' AS   Week_No,   1 AS ID UNION SELECT ' w2' AS   Week_No,   2 AS ID UNION "
    + " SELECT ' w3' AS   Week_No,   3 AS ID UNION SELECT ' w4' AS   Week_No,   4 AS ID UNION  "
+ " SELECT ' w5' AS   Week_No,   5 AS ID UNION SELECT ' w6' AS   Week_No,   6 AS ID UNION "
+ " SELECT ' w7' AS   Week_No,   7 AS ID UNION SELECT ' w8' AS   Week_No,   8 AS ID UNION "
+ " SELECT ' w9' AS   Week_No,   9 AS ID UNION SELECT ' w10' AS   Week_No,   10 AS ID UNION "
+ " SELECT ' w11' AS   Week_No,   11 AS ID UNION SELECT ' w12' AS   Week_No,   12 AS ID UNION "
+ " SELECT ' w13' AS   Week_No,   13 AS ID UNION SELECT ' w14' AS   Week_No,   14 AS ID UNION "
+ " SELECT ' w15' AS   Week_No,   15 AS ID UNION SELECT ' w16' AS   Week_No,   16 AS ID UNION "
+ " SELECT ' w17' AS   Week_No,   17 AS ID UNION SELECT ' w18' AS   Week_No,   18 AS ID UNION "
+ " SELECT ' w19' AS   Week_No,   19 AS ID UNION SELECT ' w20' AS   Week_No,   20 AS ID UNION "
+ " SELECT ' w21' AS   Week_No,   21 AS ID UNION SELECT ' w22' AS   Week_No,   22 AS ID UNION "
+ " SELECT ' w23' AS   Week_No,   23 AS ID UNION SELECT ' w24' AS   Week_No,   24 AS ID UNION "
+ " SELECT ' w25' AS   Week_No,   25 AS ID UNION SELECT ' w26' AS   Week_No,   26 AS ID UNION "
+ " SELECT ' w27' AS   Week_No,   27 AS ID UNION SELECT ' w28' AS   Week_No,   28 AS ID UNION "
+ " SELECT ' w29' AS   Week_No,   29 AS ID UNION SELECT ' w30' AS   Week_No,   30 AS ID UNION "
+ " SELECT ' w31' AS   Week_No,   31 AS ID UNION SELECT ' w32' AS   Week_No,   32 AS ID UNION "
+ " SELECT ' w33' AS   Week_No,   33 AS ID UNION SELECT ' w34' AS   Week_No,   34 AS ID UNION "
+ " SELECT ' w35' AS   Week_No,   35 AS ID UNION SELECT ' w36' AS   Week_No,   36 AS ID UNION "
+ " SELECT ' w37' AS   Week_No,   37 AS ID UNION SELECT ' w38' AS   Week_No,   38 AS ID UNION "
+ " SELECT ' w39' AS   Week_No,   39 AS ID UNION SELECT ' w40' AS   Week_No,   40 AS ID UNION "
+ " SELECT ' w41' AS   Week_No,   41 AS ID UNION SELECT ' w42' AS   Week_No,   42 AS ID UNION "
+ " SELECT ' w43' AS   Week_No,   43 AS ID UNION SELECT ' w44' AS   Week_No,   44 AS ID UNION "
+ " SELECT ' w45' AS   Week_No,   45 AS ID UNION SELECT ' w46' AS   Week_No,   46 AS ID UNION "
+ " SELECT ' w47' AS   Week_No,   47 AS ID UNION SELECT ' w48' AS   Week_No,   48 AS ID UNION "
+ " SELECT ' w49' AS   Week_No,   49 AS ID UNION SELECT ' w50' AS   Week_No,   50 AS ID UNION "
+ " SELECT ' w51' AS   Week_No,   51 AS ID UNION SELECT ' w52' AS   Week_No,   52 AS ID UNION "
+ " SELECT ' w53' AS   Week_No,   53 AS ID UNION SELECT ' w54' AS   Week_No,   54 AS ID UNION SELECT ' w55' AS   Week_No,   55 AS ID "
+ " )a "
+ " LEFT JOIN "
+ " (  "
+ " SELECT 'HP957' Product_Name,150 Volume_In_Stock, '50' Sold_Volume, 5 ID  "
+ " UNION  "
+ " SELECT 'HP957' Product,150 Volume_In_Stock, '50' Sold_Volume, 15 ID "
+ " UNION  "
+ " SELECT 'HP957' Product,150 Volume_In_Stock, '50' Sold_Volume, 30 ID  "
+ " UNION "
+ " SELECT 'HP957' Product,150 Volume_In_Stock, '50' Sold_Volume, 25 ID "
+ " UNION SELECT 'HP957' Product,150 Volume_In_Stock, '-50' Sold_Volume, 55 ID"
+ " UNION "
+ " SELECT 'P95M' Product,600 Volume_In_Stock, '250' Sold_Volume, 25 ID "
+ " UNION  "
+ " SELECT 'P95M' Product,600 Volume_In_Stock, '150' Sold_Volume, 30 ID "
+ " UNION "
+ " SELECT 'P95M' Product,600 Volume_In_Stock, '50' Sold_Volume, 25 ID "
+ " UNION SELECT 'P95M' Product,150 Volume_In_Stock, '150' Sold_Volume, 55 ID "
+ " )PRODUCT  "
+ " ON a.ID = PRODUCT.ID ";

                    dt = DBUtil.ExecuteDataTable(reportQuery);



                    // provide name of the Report xsd 
                    xsd_name = "DataSet1";

                    // add Multiple report Parameters seperated by Comma
                    report_param = new ReportParameter[] 
                    { 
                        new ReportParameter("ReportDate", DateTime.Now.ToString("dd MMM yyyy hh:mm")),
                        new ReportParameter("UserName", User.Identity.Name.ToString())
                    };


                    // provide report path
                    report_path = "Report/rptProductPlan.rdlc";
                    break;

                #endregion

                #region Consignment Report
                case "Consignment":

                    // write query to fetch report 
                    reportQuery = " SELECT '5678' AS Packing_List_no,'2014-05-28' Date_of_delivery, "
                    + " 'Ocean Freight' Mode_of_delivery,'CIF Bangkok 'Delivery_terms, "
                    + " 'Incoterms 2010'IncoTerm2010,'968, 24th Floor, "
                    + " U-Chuliang Foundation Building,Rama 4 Road, Silom, Bankrak'Delivery_Address, "
                    + " ' Bangkok 'Delivery_City, "
                    + " 'Thailand' Delivery_country, ' 10500'Delivery_Pincode, "
                    + " 'RMA14E9001-1'Order_number,' Mr.Sithichai Mahatchawaroj'Attention_Name, "
                    + " 'Mr.Sithichai Mahatchawaroj' your_Reference,'Santosh Limaye 'Our_reference, "
                    + " 'Silicon Powder, SicoMill, 4C'Description,'4000'Kgs,' 4814 kgs'Gross_weight, "
                    + " ' 4000 kgs'Net_weight,'80 drums on 14 pallet 115 x 80 x 85'Packing, "
                    + " 'Carola Strömberg Karlsson'Expideted_by,'Thai-MC Company Limited'Company_Name, "
                    + " ''Delivery_state,' 28816-28895'Drums ";


                    dt = DBUtil.ExecuteDataTable(reportQuery);



                    // provide name of the Report xsd 
                    xsd_name = "DataSet1";

                    // add Multiple report Parameters seperated by Comma
                    report_param = new ReportParameter[] 
                    { 
                        new ReportParameter("ReportDate", DateTime.Now.ToString("dd MMM yyyy"))
                    };


                    // provide report path
                    report_path = "Report/rptConsignment.rdlc";
                    break;

                #endregion

                #region Performa Invoice
                case "Performa":

                    // write query to fetch report 
                    reportQuery = "SELECT '5678' AS Invoice_Number,'2014-05-28' Date_of_delivery, "
                    + " 'Ocean Freight' Mode_of_delivery,'CIF Bangkok 'Delivery_terms, "
                    + " ''IncoTerm2010,'968, 24th Floor, "
                    + " U-Chuliang Foundation Building,Rama 4 Road, Silom, Bankrak'Delivery_Address, "
                    + " ' Bangkok 'Delivery_City, 'Thailand' Delivery_country, '10500'Delivery_Pincode, "
                    + " 'RMA14E9001-1'Order_number,' Mr.Sithichai Mahatchawaroj'Attention_Name, "
                    + " 'Mr.Sithichai Mahatchawaroj' your_Reference,'Santosh Limaye 'Our_reference, "
                    + " 'Silicon Powder, SicoMill, 4C'Description, "
                    + " 'Carola Strömberg Karlsson'Expideted_by,'Thai-MC Company Limited'Company_Name, "
                    + " ''Delivery_state, ''HSC_NO,'Sweden'Country_of_Origin, "
                    + " 'Rare Metals Department 'Attention_Department, " 
                    + " 'Rare Metals and Ferro-Alloy Division' Attention_division, "
                    + " '4000 kg á eur 9,98/kg' Weight,'EUR' Currency_type,'39,920,00' Amount ";


                    dt = DBUtil.ExecuteDataTable(reportQuery);



                    // provide name of the Report xsd 
                    xsd_name = "DataSet1";

                    // add Multiple report Parameters seperated by Comma
                    report_param = new ReportParameter[] 
                    { 
                        new ReportParameter("ReportDate", DateTime.Now.ToString("dd MMM yyyy"))
                    };


                    // provide report path
                    report_path = "Report/rptPerfomaInvoice.rdlc";
                    break;

                #endregion

                #region Performa Invoice
                case "Commercial":

                    // write query to fetch report 
                    reportQuery = "SELECT '5678' AS Invoice_Number,'2014-05-28' Date_of_delivery, "
                    + " 'Ocean Freight' Mode_of_delivery,'CIF Bangkok 'Delivery_terms, "
                    + " ''IncoTerm2010,'968, 24th Floor, "
                    + " U-Chuliang Foundation Building,Rama 4 Road, Silom, Bankrak'Delivery_Address, "
                    + " ' Bangkok 'Delivery_City, 'Thailand' Delivery_country, '10500'Delivery_Pincode, "
                    + " 'RMA14E9001-1'Order_number,' Mr.Sithichai Mahatchawaroj'Attention_Name, "
                    + " 'Mr.Sithichai Mahatchawaroj' your_Reference,'Santosh Limaye 'Our_reference, "
                    + " 'Silicon Powder, SicoMill, 4C'Description, "
                    + " 'Carola Strömberg Karlsson'Expideted_by,'Thai-MC Company Limited'Company_Name, "
                    + " ''Delivery_state, ''HSC_NO,'Sweden'Country_of_Origin, "
                    + " 'Rare Metals Department 'Attention_Department, "
                    + " 'Rare Metals and Ferro-Alloy Division' Attention_division, "
                    + " '4000 kg á eur 9,98/kg' Weight,'EUR' Currency_type,'39,920,00' Amount, "
                    + " ' 30 days net ' As Termsofpayment, '001' Clientno ";


                    dt = DBUtil.ExecuteDataTable(reportQuery);



                    // provide name of the Report xsd 
                    xsd_name = "DataSet1";

                    // add Multiple report Parameters seperated by Comma
                    report_param = new ReportParameter[] 
                    { 
                        new ReportParameter("ReportDate", DateTime.Now.ToString("dd MMM yyyy"))
                    };


                    // provide report path
                    report_path = "Report/rptCommercialInvoice.rdlc";
                    break;

                #endregion



                case "Default":
                    break;

            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    dsRpt.Name = xsd_name;
                    rptInventory.Visible = true;
                    rptInventory.LocalReport.EnableExternalImages = true;
                    dsRpt.Value = dt;
                    ReportDataSource rds = new ReportDataSource
                 (xsd_name, dt);
                    if (rptInventory.LocalReport.DataSources.Count > 0)
                    {
                        rptInventory.LocalReport.DataSources[0] = rds;
                    }
                    else
                        rptInventory.LocalReport.DataSources.Add(rds);
                    rptInventory.LocalReport.ReportPath = report_path;//path of the rdlc file
                    if (report_param != null)
                    {
                        this.rptInventory.LocalReport.SetParameters(report_param);

                    }

                    this.rptInventory.LocalReport.Refresh();
                    return;

                }
                else if (dt.Rows.Count == 0)
                {
                    // provide default path to be redirected 
                    string path = "Default.aspx";
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('No Record Found');window.location.href='" + path + "'", true);
                }
            }

        }
        catch (Exception ex)
        {

        }


    }
}
